package cn.kgc.controller;

import cn.kgc.pojo.BackendUser1;
import cn.kgc.pojo.DevUser;
import cn.kgc.service.LoginService;
import cn.kgc.tools.Constants;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {
    Logger log = Logger.getLogger(LoginController.class);

    @Resource
    private LoginService loginService;

    //登录首页
    @RequestMapping("index")
    public String index(){
        return "index";
    }

    //后台登录跳转
    @RequestMapping("backendlogin")
    public String backendlogin(){
        return "backendlogin";
    }
    //后台登录验证
    @PostMapping("dobackendlogin")
    public String dobackendlogin(@RequestParam String userCode,@RequestParam String userPassword,HttpSession session,
                                 HttpServletRequest request){

        BackendUser1 backendUser = loginService.backendUser(userCode,userPassword);
        if (backendUser != null) {
            session.setAttribute(Constants.USER_SESSION,backendUser);
            return "redirect:/login/backendmain";

        }else {
            request.setAttribute("error","账号或密码错误");
            return "backendlogin";
        }
    }

    //后台拦截
    @RequestMapping("/backendmain")
    public String backendmain(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION) == null){
            return "redirect:/login/index";
        }
        return "backend/main";
    }

    //后台注销
    @RequestMapping("backendlogout")
    public String backendlogout(HttpSession session){
        session.invalidate();
        return "redirect:/login/index";
    }

    //前台登录跳转
    @RequestMapping("devlogin")
    public String devlogin(){
        return "devlogin";
    }


    //前台登录验证
    @PostMapping("dodevlogin")
    public String dodevlogin(@RequestParam String devCode,@RequestParam String devPassword,HttpSession session,
                             HttpServletRequest request){
        DevUser devUser = loginService.devUser(devCode, devPassword);
        if (devUser != null) {
            session.setAttribute(Constants.USER_SESSION,devUser);
            return "redirect:/login/devmain";
        }else{
            request.setAttribute("error","账号或密码错误");
            return "devlogin";
        }
    }

    //前台拦截
    @RequestMapping("/devmain")
    public String devmain(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION) == null){
            return "redirect:/login/index";
        }
        return "developer/main";
    }

    //前台注销
    @RequestMapping("devlogout")
    public String devlogout(HttpSession session){
        session.invalidate();
        return "redirect:/login/index";
    }
}
